#include "ParticleManager.h"

CParticleManager* CParticleManager::s_Instance = nullptr;

CParticleManager * CParticleManager::GetInstance(void)
{

	if (s_Instance == nullptr)
		s_Instance = new CParticleManager;

	return s_Instance;
}



void CParticleManager::DeleteInstance(void)
{
	delete s_Instance;
	s_Instance = nullptr;
}


bool		CParticleManager::Initialize()
{

	////loading stuff
	//TiXmlDocument doc;


	//// Attempt to load
	//assert(doc.LoadFile());
	//TiXmlElement* pRoot = doc.RootElement();
	//if (pRoot != nullptr)
	//{
	//	TiXmlElement* xDetails = pRoot->FirstChildElement("Texture");
	//	if (xDetails != nullptr)
	//	{
	//		v_LoadedTexture.push_back(SGD::GraphicsManager::GetInstance()->LoadTexture(xDetails->GetText()));
	//	}
	//}

	//this->v_LoadedEmit.push_back();

	
	

	//this->ma_LoadedEmit.push_back(new CEmitter());
	/*emit1.m_poiPos.x = 150;
	emit1.m_poiPos.y = 150;
	emit1.SetupParticle(CEmitter::STEADY);

	emit2.m_poiPos.x = 400;
	emit2.m_poiPos.y = 150;
	emit2.SetupParticle(CEmitter::BURST);*/

	return true;
}
int		CParticleManager::Update(void)
{
	return true;
}
bool		CParticleManager::Terminate(void)
{
	for (unsigned int i = 0; i < v_LoadedEmit.size(); i++)
	{
		delete v_LoadedEmit[i];
	}
	v_LoadedEmit.clear();

	return true;
}

void CParticleManager::RenderAll()
{
	for (unsigned int i = 0; i < v_LoadedEmit.size(); i++)
	{
		v_LoadedEmit[i]->Render();
	}
}

//returns the index of where the Emitter created is. 
//You will need in order to delete it later on.
CEmitter * CParticleManager::CreateEmitter(SGD::Point* prmPoint,const char * prmFilePath, unsigned int prmEmitStyle)
{

	CEmitter * tempEmit = new CEmitter();

	tempEmit->SetupParticle(prmEmitStyle, 0, prmFilePath);

	tempEmit->m_poiPos.x = prmPoint->x;
	tempEmit->m_poiPos.y = prmPoint->y ;
	tempEmit->m_OwnerPoint = prmPoint;
	v_LoadedEmit.push_back(tempEmit);


	return tempEmit;

}
//takes in the index of the Emitter to be deleted.  
//You should have stored the return type of the CreateEmitter for this.
void CParticleManager::DeleteEmitter(CEmitter * prmEmitter)
{
	
	for (unsigned int i = 0; i < v_LoadedEmit.size(); i++)
	{
		if (prmEmitter != nullptr && prmEmitter == v_LoadedEmit[i])
		{
			delete v_LoadedEmit[i];
			v_LoadedEmit[i] = nullptr;
			v_LoadedEmit.erase(v_LoadedEmit.begin() + i);
			/*for (unsigned int resize = i; resize < v_LoadedEmit.size(); resize++)
			{
				if (v_LoadedEmit[resize + 1] != nullptr)
					v_LoadedEmit[resize] = v_LoadedEmit[resize + 1];
			}*/
			v_LoadedEmit.shrink_to_fit();
		}
	}
	
	//prmEmitter = nullptr;
}

void CParticleManager::UpdateAll(float _elapsedTime)
{
	for  (unsigned int i = 0;  i < v_LoadedEmit.size();  i++)
	{
		v_LoadedEmit[i]->Update(_elapsedTime);
	}
}